package com.jackstraw.jack_base.util.entity.mapstruct;

import com.jackstraw.jack_base.util.DateTimeUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DateMapper {
    private final SimpleDateFormat simpleDateFormat = DateTimeUtil.DATE_TIME_SIMPLE_DATE_FORMAT;

    public String asString(Date date) {
        return date != null ? DateTimeUtil.DATE_TIME_SIMPLE_DATE_FORMAT.format(date) : null;
    }

    @SneakyThrows
    public Date asDate(String date) {
        return date != null ? DateTimeUtil.DATE_TIME_SIMPLE_DATE_FORMAT.parse(date) : null;

    }

    public Date asDate(Long date) {
        return new Date(date == null ? 0 : date);
    }


    public Long asLong(Date date) {
        return date == null ? null : date.getTime();
    }
}
